package com.ycsj.goldmedalnewconcept.fragment;

import java.util.List;

import com.ycsj.goldmedalnewconcept.R;
import com.ycsj.goldmedalnewconcept.adapter.CourseGvAdapter;
import com.ycsj.goldmedalnewconcept.adapter.LazyBaseFragment;
import com.ycsj.goldmedalnewconcept.bean.CourseInfo;
import com.ycsj.goldmedalnewconcept.view.MyGridView;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.Toast;


/**
 * 创建日期：2017/11/3  10:00
 * 描述:课程进度的Framgent
 * 作者:张伟
 */

public class CourseFragment extends LazyBaseFragment {

    private static volatile CourseFragment courseFragment;
    //GridView控件
    private MyGridView mgv;
    //数据集合
    private List<CourseInfo.Data> datas;
    //GridView的适配器
    private CourseGvAdapter adapter;
    //屏幕宽度
    private int screenWidth;
    private LinearLayout llLoading;

    public static CourseFragment newInstance(CourseInfo info) {
        if (courseFragment == null) {
            synchronized (CourseFragment.courseFragment) {
                if (courseFragment == null) {
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("data", info);
                    courseFragment = new CourseFragment();
                }
            }
        }
        return courseFragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.framgent_course;
    }

    @Override
    public void lazyInit(View view, Bundle savedInstanceState) {
        //初始化控件
        mgv = (MyGridView) view.findViewById(R.id.mgv);

        //加载控件
        llLoading = (LinearLayout) view.findViewById(R.id.ll_loading);

        //获取数据
        CourseInfo info = (CourseInfo) getArguments().getSerializable("data");
        datas = info.getmDatas();

        //获取屏幕宽度
        WindowManager wm = (WindowManager) getActivity()
                .getSystemService(Context.WINDOW_SERVICE);
        screenWidth = wm.getDefaultDisplay().getWidth();

        //设置适配器
        initGv(datas);
    }

    /**
     * 设置适配器的方法
     *
     * @param datas
     */
    private void initGv(List<CourseInfo.Data> datas) {
        if (datas == null && datas.size() == 0) {
            return;
        }
        adapter = new CourseGvAdapter(getActivity(), datas, R.layout.item_mgv_course, screenWidth);
        llLoading.setVisibility(View.GONE);
        mgv.setAdapter(adapter);

//        mgv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//            @Override
//            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//                Toast.makeText(getActivity(), "被点击了", Toast.LENGTH_SHORT).show();
//            }
//        });
    }
}
